package com.ruoyi.framework.security.service;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysTabbar;
import com.ruoyi.project.system.mapper.SysRoleMapper;
import com.ruoyi.project.system.service.ISysTabbarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMenuService;
import com.ruoyi.project.system.service.ISysRoleService;

/**
 * 用户权限处理
 *
 * @author ruoyi
 */
@Component
public class SysPermissionService {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private ISysTabbarService tabbarService;


    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            List<SysRole> roles = user.getRoles();
            if (!CollectionUtils.isEmpty(roles)) {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles) {
                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            } else {
                perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
            }
        }
        return perms;
    }


    public List<SysTabbar> getTabbarPermission(Long userId) {
        List<SysTabbar> tabbars = new ArrayList<>();
        List<SysRole> roles = roleMapper.selectRolePermissionByUserId(userId);
        List<String> tabbarIds = new ArrayList<>();
        List<String> distinctList = new ArrayList<>();
        for (SysRole role : roles) {
            if (StringUtils.isNotEmpty(role.getTabbar())) {
                tabbarIds.addAll(Arrays.stream(role.getTabbar().split(","))
                        .collect(Collectors.toList()));
            }
        }
        if (tabbarIds.size() > 0) {
            distinctList = tabbarIds.stream().distinct().collect(Collectors.toList());
            for (String id : distinctList) {
                tabbars.add(tabbarService.selectSysTabbarById(Long.valueOf(id)));
            }
            List<SysTabbar> sorts = tabbars.stream()
                    .sorted(Comparator.comparingInt(SysTabbar::getSort))
                    .collect(Collectors.toList());
            return sorts;
        } else {
            return tabbars;
        }
    }


}
